package com.jxust.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 *  全局Filter
 * gateway网关实现全局自定义过滤器
 * @author tyeerth
 * @date 2020/6/22 - 10:17
 */
@Component
@Slf4j
public class MylogGatewayFilter implements GlobalFilter, Ordered {
    @Override
    /**
     * 做一个全局过滤器，判断每次是否携带正确的username
     */
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        log.info("***************come in MylogGatewayFilter"+ new Date());
//        String username = exchange.getRequest().getQueryParams().getFirst("username");
//        if (username == null){
//            log.info("同学，不好意思******，用户名为空");
//            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
//            return exchange.getResponse().setComplete();
//        }
        //否则放行
        return chain.filter(exchange);
    }

    /**
     * 全局加载过滤器的的执行顺序。
     * 0表示是在第一位
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
